const Dnode = require('./dnode');

class DoublyCircleLinkedList {
    // 构造器
    constructor(head = new Dnode(null, this.head, this.head)) {
        this.head = head
    }
    // 静态数组构造
    static createByArr(arr) {
        let dcll = new DoublyCircleLinkedList()
        let p = dcll.head
        for (let i = 0; i <= arr.length - 1; i++) {
            p.next = new Dnode(arr[i], p, null)
            p = p.next
        }
        p.next = dcll.head
        dcll.head.prev = p
        return dcll
    }
    // 返回表长
    listSize() {
        let count = 0
        let p = this.head
        while (p.next !== null) {
            count++
            p = p.next
        }
        return count
    }
    // 返回描述字符串
    toString() {
        let str = `toTail<--`
        let p = this.head
        while(p.next !== this.head) {
            str += `${p.toString()}<-->`
            p = p.next
        }
        console.log(str.length);
        str = str.slice(0,str.length-4)
        return `${str}-->toHead`
    }
}

module.exports = DoublyCircleLinkedList;





// 测试用例
// let arr = [1,2,3,4,5,6]
// let dcll = DoublyCircleLinkedList.createByArr(arr)
// console.log(dcll.toString());